﻿using CinemaMaestro.ViewModels;
using CinemaMaestro.WindowsApplication.EndPoints;
using CinemaMaestro.WindowsApplication.Room;
using MetroFramework;
using MetroFramework.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CinemaMaestro.WindowsApplication.Cinema
{
    public partial class CinemasForm : MetroForm
    {
        private CinemaEndPoints endPoints;
        private HttpClient client;

        public CinemasForm(HttpClient client)
        {
            this.endPoints = new CinemaEndPoints(client);
            this.client = client;
            InitializeComponent();
            PopulateCinemasListView();

            deleteButton.Enabled = false;
            updateButton.Enabled = false;
            roomsButton.Enabled = false;
        }

        private async void PopulateCinemasListView()
        {
            cinemasListView.Items.Clear();
            try
            {
                List<CinemaViewModel> cinemas = await endPoints.GetAllCinemasAsync();
                foreach (var cinema in cinemas)
                {
                    var item = new ListViewItem();
                    item.Text = cinema.Id.ToString();
                    item.SubItems.Add(cinema.Name);
                    item.SubItems.Add(cinema.Address);
                    item.SubItems.Add(cinema.TelephoneNr);
                    cinemasListView.Items.Add(item);
                }
            }
            catch
            {
                MetroMessageBox.Show(this, "Cannot retrieve cinemas!", String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }         
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            nameTextBox.Text = String.Empty;
            addressTextBox.Text = String.Empty;
            phoneTextBox.Text = String.Empty;
            cinemasListView.SelectedIndices.Clear();
            deleteButton.Enabled = false;
            updateButton.Enabled = false;
            roomsButton.Enabled = false;
            createButton.Enabled = true;
        }

        private async void createButton_Click(object sender, EventArgs e)
        {
            if (ValidateCinema())
            {
                try
                {
                    var cinema = new CinemaViewModel
                    { 
                        Name = nameTextBox.Text,
                        Address = addressTextBox.Text,
                        TelephoneNr = phoneTextBox.Text
                    };
                    await endPoints.CreateCinemaAsync(cinema);
                }
                catch
                {
                    MetroMessageBox.Show(this, "Cannot create cinema!", String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                finally
                {
                    PopulateCinemasListView();
                    nameTextBox.Text = String.Empty;
                    addressTextBox.Text = String.Empty;
                    phoneTextBox.Text = String.Empty;
                }         
            }           
        }

        private void cinemasListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cinemasListView.SelectedItems.Count == 1)
            {
                deleteButton.Enabled = true;
                updateButton.Enabled = true;
                roomsButton.Enabled = true;
                createButton.Enabled = false;
                nameTextBox.Text = cinemasListView.SelectedItems[0].SubItems[1].Text;
                addressTextBox.Text = cinemasListView.SelectedItems[0].SubItems[2].Text;
                phoneTextBox.Text = cinemasListView.SelectedItems[0].SubItems[3].Text;
            }
        }

        private async void updateButton_Click(object sender, EventArgs e)
        {
            
            deleteButton.Enabled = false;
            updateButton.Enabled = false;
            roomsButton.Enabled = false;
            if (ValidateCinema())
            {
                try
                {
                    int selectedId = Int32.Parse(cinemasListView.SelectedItems[0].SubItems[0].Text);
                    var cinema = new CinemaViewModel
                    {
                        Id = selectedId,
                        Name = nameTextBox.Text,
                        Address = addressTextBox.Text,
                        TelephoneNr = phoneTextBox.Text
                    };
                    await endPoints.UpdateCinemaAsync(cinema);
                }
                catch
                {
                    MetroMessageBox.Show(this, "Cannot update cinema!", String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                finally
                {
                    PopulateCinemasListView();
                    cinemasListView.SelectedIndices.Clear();
                    nameTextBox.Text = String.Empty;
                    createButton.Enabled = true;
                    addressTextBox.Text = String.Empty;
                    phoneTextBox.Text = String.Empty;
                }   
            }          
        }

        private void CinemasForm_MouseDown(object sender, MouseEventArgs e)
        {
            cinemasListView.SelectedIndices.Clear();
            deleteButton.Enabled = false;
            updateButton.Enabled = false;
            roomsButton.Enabled = false;
            createButton.Enabled = true;
            nameTextBox.Text = String.Empty;
            addressTextBox.Text = String.Empty;
            phoneTextBox.Text = String.Empty;
        }

        private async void deleteButton_Click(object sender, EventArgs e)
        {
            try
            {
                deleteButton.Enabled = false;
                updateButton.Enabled = false;
                roomsButton.Enabled = false;
                int selectedId = Int32.Parse(cinemasListView.SelectedItems[0].SubItems[0].Text);
                await endPoints.DeleteCinemaAsync(selectedId);
            }
            catch
            {
                MetroMessageBox.Show(this, "Cannot delete cinema!", String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            finally
            {
                PopulateCinemasListView();
                cinemasListView.SelectedIndices.Clear();
                nameTextBox.Text = String.Empty;
                addressTextBox.Text = String.Empty;
                phoneTextBox.Text = String.Empty;
                createButton.Enabled = true;
            }           
        }

        private bool ValidateCinema()
        {
            if (nameTextBox.Text == String.Empty)
            {
                MetroMessageBox.Show(this, "Please provide a name for the Cinema!", String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (addressTextBox.Text == String.Empty)
            {
                MetroMessageBox.Show(this, "Please provide an address for the Cinema!", String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (phoneTextBox.Text == String.Empty)
            {
                MetroMessageBox.Show(this, "Please provide a phone number for the Cinema!", String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            return true;
        }

        private void roomsButton_Click(object sender, EventArgs e)
        {
            CinemaViewModel selectedCinema = new CinemaViewModel()
            {
                Id = Int32.Parse(cinemasListView.SelectedItems[0].SubItems[0].Text),
                Name = cinemasListView.SelectedItems[0].SubItems[1].Text,
                Address = cinemasListView.SelectedItems[0].SubItems[2].Text,
                TelephoneNr = cinemasListView.SelectedItems[0].SubItems[3].Text
            };
            RoomsForm form = new RoomsForm(client, selectedCinema);
            form.ShowDialog();
        }
    }
}
